<template>
  <div class="logistics-orders">
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span class="title">物流订单管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新建物流订单
          </el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="订单编号">
          <el-input v-model="searchForm.orderNo" placeholder="请输入订单编号" clearable />
        </el-form-item>
        <el-form-item label="物流状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="待发货" value="pending" />
            <el-option label="运输中" value="shipping" />
            <el-option label="派送中" value="delivering" />
            <el-option label="已签收" value="signed" />
            <el-option label="异常" value="exception" />
          </el-select>
        </el-form-item>
        <el-form-item label="承运商">
          <el-select v-model="searchForm.carrier" placeholder="请选择承运商" clearable>
            <el-option label="顺丰速运" value="sf" />
            <el-option label="圆通速递" value="yt" />
            <el-option label="中通快递" value="zt" />
            <el-option label="韵达快递" value="yd" />
            <el-option label="申通快递" value="st" />
          </el-select>
        </el-form-item>
        <el-form-item label="日期范围">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table :data="tableData" v-loading="loading" stripe>
        <el-table-column prop="orderNo" label="物流订单号" width="180" />
        <el-table-column prop="trackingNo" label="运单号" width="180" />
        <el-table-column prop="carrier" label="承运商" width="120" />
        <el-table-column prop="senderName" label="发件人" width="120" />
        <el-table-column prop="receiverName" label="收件人" width="120" />
        <el-table-column prop="receiverPhone" label="联系电话" width="120" />
        <el-table-column prop="destination" label="目的地" width="150" />
        <el-table-column prop="status" label="物流状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">{{ getStatusText(row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="freight" label="运费(元)" width="100" align="right" />
        <el-table-column prop="createTime" label="创建时间" width="160" />
        <el-table-column label="操作" width="240" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" size="small" @click="handleView(row)">
              <el-icon><View /></el-icon>
              详情
            </el-button>
            <el-button link type="primary" size="small" @click="handleTrack(row)">
              <el-icon><Position /></el-icon>
              跟踪
            </el-button>
            <el-button link type="primary" size="small" @click="handleEdit(row)">
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
            <el-button link type="danger" size="small" @click="handleDelete(row)">
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="800px"
      @close="handleDialogClose"
    >
      <el-form :model="formData" :rules="rules" ref="formRef" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="承运商" prop="carrier">
              <el-select v-model="formData.carrier" placeholder="请选择承运商">
                <el-option label="顺丰速运" value="sf" />
                <el-option label="圆通速递" value="yt" />
                <el-option label="中通快递" value="zt" />
                <el-option label="韵达快递" value="yd" />
                <el-option label="申通快递" value="st" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="运单号" prop="trackingNo">
              <el-input v-model="formData.trackingNo" placeholder="请输入运单号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">发件信息</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="发件人" prop="senderName">
              <el-input v-model="formData.senderName" placeholder="请输入发件人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="senderPhone">
              <el-input v-model="formData.senderPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="发件地址" prop="senderAddress">
              <el-input v-model="formData.senderAddress" placeholder="请输入发件地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">收件信息</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="收件人" prop="receiverName">
              <el-input v-model="formData.receiverName" placeholder="请输入收件人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="receiverPhone">
              <el-input v-model="formData.receiverPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="收件地址" prop="receiverAddress">
              <el-input v-model="formData.receiverAddress" placeholder="请输入收件地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">货物信息</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="货物名称" prop="goodsName">
              <el-input v-model="formData.goodsName" placeholder="请输入货物名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="重量(kg)" prop="weight">
              <el-input-number v-model="formData.weight" :min="0" :precision="2" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="运费(元)" prop="freight">
              <el-input-number v-model="formData.freight" :min="0" :precision="2" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款方式" prop="paymentType">
              <el-select v-model="formData.paymentType" placeholder="请选择付款方式">
                <el-option label="寄付" value="prepaid" />
                <el-option label="到付" value="collect" />
                <el-option label="月结" value="monthly" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" type="textarea" :rows="3" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Plus, Search, Refresh, Edit, Delete, View, Position
} from '@element-plus/icons-vue'

// 搜索表单
const searchForm = reactive({
  orderNo: '',
  status: '',
  carrier: '',
  dateRange: null
})

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0
})

// 对话框
const dialogVisible = ref(false)
const dialogTitle = ref('新建物流订单')
const formRef = ref(null)

// 表单数据
const formData = reactive({
  carrier: '',
  trackingNo: '',
  senderName: '',
  senderPhone: '',
  senderAddress: '',
  receiverName: '',
  receiverPhone: '',
  receiverAddress: '',
  goodsName: '',
  weight: 0,
  freight: 0,
  paymentType: '',
  remark: ''
})

// 表单验证规则
const rules = {
  carrier: [{ required: true, message: '请选择承运商', trigger: 'change' }],
  trackingNo: [{ required: true, message: '请输入运单号', trigger: 'blur' }],
  senderName: [{ required: true, message: '请输入发件人姓名', trigger: 'blur' }],
  senderPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
  senderAddress: [{ required: true, message: '请输入发件地址', trigger: 'blur' }],
  receiverName: [{ required: true, message: '请输入收件人姓名', trigger: 'blur' }],
  receiverPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
  receiverAddress: [{ required: true, message: '请输入收件地址', trigger: 'blur' }],
  goodsName: [{ required: true, message: '请输入货物名称', trigger: 'blur' }],
  paymentType: [{ required: true, message: '请选择付款方式', trigger: 'change' }]
}

// 获取状态类型
const getStatusType = (status) => {
  const typeMap = {
    pending: 'info',
    shipping: 'primary',
    delivering: 'warning',
    signed: 'success',
    exception: 'danger'
  }
  return typeMap[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const textMap = {
    pending: '待发货',
    shipping: '运输中',
    delivering: '派送中',
    signed: '已签收',
    exception: '异常'
  }
  return textMap[status] || status
}

// 加载数据
const loadData = () => {
  loading.value = true
  
  // 模拟数据
  setTimeout(() => {
    tableData.value = [
      {
        id: 1,
        orderNo: 'LG2024010001',
        trackingNo: 'SF1234567890',
        carrier: '顺丰速运',
        senderName: '张三',
        receiverName: '李四',
        receiverPhone: '13800138000',
        destination: '北京市朝阳区',
        status: 'shipping',
        freight: 15.00,
        createTime: '2024-01-01 10:00:00'
      },
      {
        id: 2,
        orderNo: 'LG2024010002',
        trackingNo: 'YT9876543210',
        carrier: '圆通速递',
        senderName: '王五',
        receiverName: '赵六',
        receiverPhone: '13900139000',
        destination: '上海市浦东新区',
        status: 'delivering',
        freight: 12.00,
        createTime: '2024-01-02 11:30:00'
      }
    ]
    pagination.total = 2
    loading.value = false
  }, 500)
}

// 搜索
const handleSearch = () => {
  pagination.page = 1
  loadData()
}

// 重置
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = key === 'dateRange' ? null : ''
  })
  handleSearch()
}

// 新增
const handleAdd = () => {
  dialogTitle.value = '新建物流订单'
  dialogVisible.value = true
}

// 查看详情
const handleView = (row) => {
  ElMessage.info(`查看物流订单: ${row.orderNo}`)
}

// 跟踪
const handleTrack = (row) => {
  ElMessage.info(`跟踪物流: ${row.trackingNo}`)
}

// 编辑
const handleEdit = (row) => {
  dialogTitle.value = '编辑物流订单'
  Object.assign(formData, row)
  dialogVisible.value = true
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该物流订单吗？', '提示', {
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    loadData()
  })
}

// 提交表单
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success(dialogTitle.value === '新建物流订单' ? '创建成功' : '更新成功')
      dialogVisible.value = false
      loadData()
    }
  })
}

// 对话框关闭
const handleDialogClose = () => {
  formRef.value?.resetFields()
}

// 分页改变
const handleSizeChange = () => {
  loadData()
}

const handleCurrentChange = () => {
  loadData()
}

onMounted(() => {
  loadData()
})
</script>

<style scoped lang="scss">
.logistics-orders {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      font-size: 16px;
      font-weight: 500;
    }
  }

  .search-form {
    margin-bottom: 20px;
  }

  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>

